package com.takeeasy.platform.util;

public class DBUtil {
	public static synchronized String modifyColumnName(String columnName) {
		String[] names = columnName.split("_");
		StringBuffer nameBuffer = new StringBuffer();
		for (String name : names) {
			String nameFirstWord = name.substring(0, 1).toUpperCase();
			String nameFull = nameFirstWord + name.substring(1, name.length());
			nameBuffer.append(nameFull);
		}
		String finalName = nameBuffer.toString().substring(0, 1).toLowerCase() + nameBuffer.toString().substring(1, nameBuffer.toString().length());
		return finalName;
	}

	public static synchronized String addSQLParameter(String sql, String key,String expr) {
		if (sql == null || "".equals(sql) || key == null || "".equals(key)) {
			return sql;
		}
		int whereIndex = sql.indexOf("where");
		StringBuffer sb = new StringBuffer(sql);
		if (whereIndex == -1) {
			sb.append(" where ");
			sb.append(key);
			sb.append(" = ?");
			sql = sb.toString();
		} else {
			sb.append(" ");
			sb.append((expr == null || "".equals(expr)) ? "and" : expr);
			sb.append(" ");
			sb.append(key);
			sb.append(" = ?");
			sql = sb.toString();
		}
		return sql;
	}
}
